/*
 * Game.cpp
 *
 *  Created on: Sep 22, 2014
 *      Author: Administrator
 */
#include "GameScene.h"

USING_NS_CC;

Scene* GameScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = GameScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance

bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto* bg  = Sprite::create("image/board_back_01.png");
    int x = 240, y = 400;
    bg->setPosition(x,y);
    bg->setScale(1.5);
    this->addChild(bg);



    // end 400,140

    CCLOG("Begin");
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan,this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);

    return true;
}
std::vector<int> arrays[7] ;
bool play = true;
int player = 1;
bool GameScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event){
	if (play == false) return 0;
	player = 3 - player;
	play = false;
	CCLOG("On touch Began in x = %f, y = %f ",touch->getLocation().x,touch->getLocation().y);
	// 400 140
	float x = touch->getLocation().x;

	int pos = (x - 57)/60;
	if (pos < 0) pos = 0;
	if (pos > 6) pos = 6;
	CCLOG("POS = %d",pos);
	if (arrays[pos].size() < 6){
		GameScene::createBall(59+(pos*40*1.5),252 + (arrays[pos].size()*60),30-(arrays[pos].size()*(30/6)));
		arrays[pos].push_back(1);
	}
	return 0;
}

void GameScene::createBall(float x,float end_y,float jumpHeight){
	Sprite* ball;
	if (player == 1)
		ball = Sprite::create("ball/p_01_w_norm.png");
	else
		ball = Sprite::create("ball/p_04_b_norm.png");
	ball->setPosition(x,613);
	ball->setScale(0.9);
	this->addChild(ball);

	auto action1 = JumpTo::create(0.6,Point(x,end_y),40,1);
	auto action2 = MoveBy::create(0.2,Point(0,jumpHeight));
	auto action3 = MoveBy::create(0.2,Point(0,-jumpHeight));
	auto callback = CallFunc::create( [this]() {
		GameScene::actionFinished();
	});
	auto sequence = Sequence::create(action1,action2,action3,callback,NULL);
	ball->runAction(sequence);

}
void GameScene::actionFinished(){
	play = true;
}

void GameScene::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}





